package com.example.camerademo.listview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.example.camerademo.R;

/**
 * author: wj
 * created on: 2021/4/2 9:40
 * description:
 */
public class MyListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    private static String[] titles = {
            "花", "dog", "幻灯片背景", "花", "菜花图片", "狐狸", "梁洁", "九寨沟雨景", "快乐的小燕子", "竹林", "舒畅"
    };

    private static String[] times = {
            "2021-04-02", "2021-04-02", "2021-04-02", "2021-04-02", "2021-04-02", "2021-04-02",
            "2021-04-02", "2021-04-02", "2021-04-02", "2021-04-02", "2021-04-02"
    };

    private static String[] contents = {
            "https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1361135963,570304265&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3681880960,455182084&fm=26&gp=0.jpg",
            "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3791918726,2864900975&fm=26&gp=0.jpg",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1402%2F07%2Fc7%2F31066355_31066355_1391779709500_mthumb.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1619944099&t=ed7ef2bb98567e37b08daaf4e11b77a2",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2474992498,2867548152&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3365503159,1655500687&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3421385601,1521752733&fm=11&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2526580938,627798856&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1514254201,1610764859&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3347012385,1786653918&fm=26&gp=0.jpg",
            "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1058377784,4109878848&fm=26&gp=0.jpg"

    };

    /**
     * MyListAdapter的构造方法，一个Content类型的参数，也就是哪一个activity
     * @param context
     */
    public MyListAdapter(Context context) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    // 写一个静态的class，把layout_list_item的控件转移过来使用
    static class ViewHolder{
        public ImageView imageView;
        public TextView tvTitle, tvTime, tvContext;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // 实例化ViewHolder
        ViewHolder holder = null;
        //如果视图为空
        if (convertView == null) {
            //此处需要导入包，填写ListView的图标和标题等控件的来源，来自于layout_list_item这个布局文件
            convertView = mLayoutInflater.inflate(R.layout.layout_list_item,null);
            //生成一个ViewHolder的对象
            holder = new ViewHolder();
            //把layout_list_item对象转移过来，以便修改和赋值
            holder.imageView = convertView.findViewById(R.id.IV_list_id);
            holder.tvTitle = convertView.findViewById(R.id.TV_listTitle_id);
            holder.tvTime = convertView.findViewById(R.id.TV_listTime_id);
            holder.tvContext = convertView.findViewById(R.id.TV_listContext_id);
            // 把这个holder传递进去
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //给控件赋值
        holder.tvTitle.setText(titles[position]);
        holder.tvTime.setText(times[position]);
        holder.tvContext.setText("图片地址：" + contents[position]);
        Glide.with(mContext).load(contents[position]).into(holder.imageView);
        return convertView;
    }
}


